import java.util.Arrays;

public class Solution135 {
    public int candy(int[] ratings) {
        int n = ratings.length;
        int res = 0;
        int[] nums = new int[n];
        Arrays.fill(nums, 1);
        for (int i = 1; i < n; i++) {
            if (ratings[i] > ratings[i - 1]) {
                nums[i] = nums[i - 1] + 1;
            }
        }
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i + 1] < ratings[i]) {
                nums[i] = Math.max(nums[i], nums[i + 1] + 1);
            }
        }
        for (int num : nums) {
            res += num;
        }
        return res;
    }
}
